/* Copyright (c) 2006, Jan Flora <janflora@diku.dk>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the University of Copenhagen nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
  @author Jan Flora <janflora@diku.dk>
*/

module ADTBufferManM
{
	provides
	{
		interface StdControl;
		interface MemoryManager as BufferMng;
	}
	uses
	{
		interface FIFOQueue as Queue;
		interface Debug;
	}
}

#define POOL_SIZE_ADDR 10
#define POOL_SIZE_SDU 5
#define POOL_SIZE_PIB 5

#define MAX_PIB_SIZE 16

implementation
{
	#define DBG_LEVEL 3
	#include "Debug.h"

	uint8_t bufPoolAddr[POOL_SIZE_ADDR][sizeof(ieeeAddress_t)];
	uint8_t *queueBufferAddr[POOL_SIZE_ADDR];
	uint8_t bufPoolSdu[POOL_SIZE_SDU][sizeof(fsSdu_t)];
	uint8_t *queueBufferSdu[POOL_SIZE_SDU];
	uint8_t bufPoolPib[POOL_SIZE_PIB][MAX_PIB_SIZE];
	uint8_t *queueBufferPib[POOL_SIZE_PIB];

	// Queues that keep pointers to free memory blocks.
	FIFOQueue_t freeQueueAddr;
	FIFOQueue_t freeQueueSdu;
	FIFOQueue_t freeQueuePib;

	// Forward declarations
	FIFOQueue_t *getPool(uint8_t size);

	command result_t StdControl.init()
	{
		uint8_t i;
		// Initialize free queues.
		call Queue.initQueue(&freeQueueAddr, queueBufferAddr, POOL_SIZE_ADDR);
		call Queue.initQueue(&freeQueueSdu, queueBufferSdu, POOL_SIZE_SDU);
		call Queue.initQueue(&freeQueuePib, queueBufferPib, POOL_SIZE_PIB);
		
		// Add all buffer pool elements to the free queues.
		for (i=0;i<POOL_SIZE_ADDR;i++) {
			call Queue.enqueue(&freeQueueAddr, bufPoolAddr[i]);
		}
		for (i=0;i<POOL_SIZE_SDU;i++) {
			call Queue.enqueue(&freeQueueSdu, bufPoolSdu[i]);
		}
		for (i=0;i<POOL_SIZE_PIB;i++) {
			call Queue.enqueue(&freeQueuePib, bufPoolPib[i]);
		}
		return SUCCESS;
	}
	
	command result_t StdControl.start()
	{
		return SUCCESS;
	}
	
	command result_t StdControl.stop()
	{
		return SUCCESS;
	}

	async command result_t BufferMng.claim( uint8_t size, uint8_t **buffer )
	{
		
		FIFOQueue_t *freeQueue;

		if (size > MAX_PIB_SIZE) return FAIL;		
		freeQueue = getPool(size);
		return call Queue.dequeue(freeQueue, buffer);
	}
	
	async command result_t BufferMng.release( uint8_t size, uint8_t *buffer )
	{
		FIFOQueue_t *freeQueue;
		
		if (size > MAX_PIB_SIZE) return FAIL;
		freeQueue = getPool(size);
		return call Queue.enqueue(freeQueue, buffer);
	}

	FIFOQueue_t *getPool(uint8_t size)
	{
		if (size == sizeof(ieeeAddress_t)) {
			return &freeQueueAddr;
		}
		if (size == sizeof(fsSdu_t)) {
			return &freeQueueSdu;
		}
		return &freeQueuePib;
	}
}
